package soft.quicknote.activity;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import soft.quicknote.R;

public class UpdatePassword extends AppCompatActivity {

    private final static String PWD_KEY = "password";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
    }


    public void confirmUpdatePassword(View view) {
        String oldPwd = ((EditText) findViewById(R.id.editText_oldpwd)).getText().toString();
        String newPwd = ((EditText) findViewById(R.id.editText_newpwd)).getText().toString();

        SharedPreferences sp = getSharedPreferences("quicknote", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String curPwd = getSharedPreferences("quicknote", MODE_PRIVATE).getString(PWD_KEY, "");
        if (curPwd.equals("")) {
            editor.putString(PWD_KEY, newPwd);
            editor.commit();
            Toast.makeText(this, "首次设置密码成功！", Toast.LENGTH_LONG).show();
            finish();
        }
        else if(curPwd.equals(oldPwd)) {
            if(newPwd.equals("")){
                Toast.makeText(this, "新密码不能为空！", Toast.LENGTH_LONG).show();
            }else {
                editor.putString(PWD_KEY, newPwd);
                editor.commit();
                Toast.makeText(this, "更新密码成功！", Toast.LENGTH_LONG).show();
                finish();
            }
        }
        else{
            Toast.makeText(this, "旧密码不正确！", Toast.LENGTH_LONG).show();
        }


    }
}
